{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-04-26T09:37:14.929542Z",
     "start_time": "2020-04-26T09:37:14.455885Z"
    }
   },
   "outputs": [],
   "source": [
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-04-26T09:40:04.772808Z",
     "start_time": "2020-04-26T09:40:04.761062Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[ 8, 16,  3,  5],\n",
       "         [10, 12, 17, 23],\n",
       "         [ 1,  3,  6,  7]],\n",
       "\n",
       "        [[14, 11,  6, 18],\n",
       "         [ 8, 16, 14, 15],\n",
       "         [ 9, 13,  2,  5]]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "randi234 = torch.randint(low=1, high=24, size=(2,3,4))\n",
    "randi234 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-04-24T08:28:50.437130Z",
     "start_time": "2020-04-24T08:28:50.419557Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(torch.Size([4, 3, 28, 28]),\n",
       " torch.Size([3, 28, 28]),\n",
       " torch.Size([28, 28]),\n",
       " tensor([0.6694, 0.8256, 0.7489, 0.1397, 0.7875, 0.7043, 0.1142, 0.3358, 0.5136,\n",
       "         0.1293, 0.4332, 0.5507, 0.9628, 0.9811, 0.7141, 0.8963, 0.9787, 0.3335,\n",
       "         0.6848, 0.6762, 0.0068, 0.0010, 0.3885, 0.0409, 0.3395, 0.9032, 0.9632,\n",
       "         0.6162]))"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "batch_pics = torch.rand(4, 3, 28, 28)\n",
    "batch_pics.shape, batch_pics[0].shape, batch_pics[0, 0].shape, batch_pics[0, 0, 0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-04-24T08:10:59.928599Z",
     "start_time": "2020-04-24T08:10:59.913818Z"
    }
   },
   "source": [
    "# 取第一、第二张图片中的第一个通道"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-04-24T08:28:50.447139Z",
     "start_time": "2020-04-24T08:28:50.440307Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([2, 28, 28])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "batch_pics[:2, 1, :, :].shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 取第一、第二张图片中的第二个通道与第三个通道"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-04-24T08:28:50.459777Z",
     "start_time": "2020-04-24T08:28:50.449745Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([2, 2, 28, 28])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "batch_pics[:2, 1:, :, :].shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 取第一、第二张图片中的最后两个通道"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-04-24T08:28:50.471318Z",
     "start_time": "2020-04-24T08:28:50.462345Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([2, 2, 28, 28])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "batch_pics[:2, -2:, :, :].shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第三、第四维度隔行采样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-04-24T08:28:50.483866Z",
     "start_time": "2020-04-24T08:28:50.473949Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([4, 3, 14, 14])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "batch_pics[:, :, 0:28:2, 0:28:2].shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-04-24T08:28:50.494652Z",
     "start_time": "2020-04-24T08:28:50.486295Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([4, 3, 14, 14])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "batch_pics[:, :, ::2, ::2].shape # 同上"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 索引任意多的维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-04-24T08:28:50.507080Z",
     "start_time": "2020-04-24T08:28:50.498054Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([4, 3, 28, 28])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "batch_pics[...].shape # == batch_pics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-04-24T08:28:50.518017Z",
     "start_time": "2020-04-24T08:28:50.510366Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([3, 28, 28])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "batch_pics[0, ...].shape # 第一张图片的所有维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-04-24T08:28:50.530233Z",
     "start_time": "2020-04-24T08:28:50.520963Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([4, 28, 28])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "batch_pics[:, 1, ...].shape # 所有图片第二通道的所有维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-04-24T08:28:50.541783Z",
     "start_time": "2020-04-24T08:28:50.533620Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([4, 3, 28, 2])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "batch_pics[..., :2].shape # 所有图像所有通道所有行的第一、第二列"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# mask索引 (结果被打平Flatten)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-04-24T08:28:50.553331Z",
     "start_time": "2020-04-24T08:28:50.544378Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[ 0.3337,  0.6265, -0.0600,  0.7349],\n",
       "         [-2.5840,  0.1514,  0.6360,  1.0580],\n",
       "         [ 0.8850,  0.3289, -1.0753, -0.3955]]),\n",
       " tensor([[False,  True, False,  True],\n",
       "         [False, False,  True,  True],\n",
       "         [ True, False, False, False]]),\n",
       " torch.Size([3, 4]))"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = torch.randn(3, 4)\n",
    "mask = data.ge(0.5)\n",
    "data, mask, mask.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-04-24T08:28:50.587664Z",
     "start_time": "2020-04-24T08:28:50.555474Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([0.6265, 0.7349, 0.6360, 1.0580, 0.8850]), torch.Size([5]))"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result = torch.masked_select(data, mask)\n",
    "result, result.shape # 被打平"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# take索引 (先打平在索引)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-04-24T08:28:50.600970Z",
     "start_time": "2020-04-24T08:28:50.590269Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([ 0.3337,  0.6265, -0.3955]), torch.Size([3]))"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result = torch.take(data, torch.tensor([0, 1, -1]))\n",
    "result, result.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 嵌套[ ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-04-26T09:40:31.707435Z",
     "start_time": "2020-04-26T09:40:31.697240Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[ 8, 16,  3,  5],\n",
       "         [10, 12, 17, 23],\n",
       "         [ 1,  3,  6,  7]],\n",
       "\n",
       "        [[14, 11,  6, 18],\n",
       "         [ 8, 16, 14, 15],\n",
       "         [ 9, 13,  2,  5]]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = randi234\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-04-26T09:41:28.843363Z",
     "start_time": "2020-04-26T09:41:28.833036Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[ 8, 16,  3,  5],\n",
       "         [14, 11,  6, 18]]),\n",
       " tensor([[[ 8, 16,  3,  5]],\n",
       " \n",
       "         [[14, 11,  6, 18]]]))"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[:, 0], data[:, [0]] # 结果放入到[]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-04-26T09:42:27.744868Z",
     "start_time": "2020-04-26T09:42:27.734097Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([ 8, 14]),\n",
       " tensor([[ 8],\n",
       "         [14]]))"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[:, 0, 0], data[:, 0, [0]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-04-26T09:43:37.815514Z",
     "start_time": "2020-04-26T09:43:37.804973Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[ 8, 10,  1],\n",
       "         [14,  8,  9]]),\n",
       " tensor([[[ 8],\n",
       "          [10],\n",
       "          [ 1]],\n",
       " \n",
       "         [[14],\n",
       "          [ 8],\n",
       "          [ 9]]]))"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[:, :, 0], data[:, :, [0]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# References\n",
    "\n",
    "[Pytorch Tensor的索引与切片][1]\n",
    "\n",
    "[1]: https://blog.csdn.net/weicao1990/article/details/93599947"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
